load("@rules_jvm_external//:kt_defs.bzl", "kt_jvm_export")
load("@rules_kotlin//kotlin:jvm.bzl", "kt_jvm_library")
load("@rules_pkg//pkg:mappings.bzl", "pkg_files", "strip_prefix")
load("//:protobuf.bzl", "internal_gen_kt_protos")
load("//:protobuf_version.bzl", "PROTOBUF_JAVA_VERSION")
load("//bazel:java_proto_library.bzl", "java_proto_library")
load("//bazel:proto_library.bzl", "proto_library")
load("//build_defs:kotlin_opts.bzl", "protobuf_versioned_kt_jvm_library")

exports_files([
    "src/test/kotlin/com/google/protobuf/Proto3Test.kt",
])

# Kotlin generated protos depend on this and only this.
kt_jvm_library(
    name = "shared_runtime",
    srcs = [
        "src/main/kotlin/com/google/protobuf/DslList.kt",
        "src/main/kotlin/com/google/protobuf/DslMap.kt",
        "src/main/kotlin/com/google/protobuf/DslProxy.kt",
        "src/main/kotlin/com/google/protobuf/ExtensionList.kt",
        "src/main/kotlin/com/google/protobuf/ProtoDslMarker.kt",
        "src/main/kotlin/com/google/protobuf/UnmodifiableCollections.kt",
    ],
    visibility = ["//visibility:public"],
    deps = [
        ":only_for_use_in_proto_generated_code_its_generator_and_tests",
        "//java/lite",
    ],
)

kt_jvm_library(
    name = "only_for_use_in_proto_generated_code_its_generator_and_tests",
    srcs = ["src/main/kotlin/com/google/protobuf/OnlyForUseByGeneratedProtoCode.kt"],
    visibility = ["//java:__subpackages__"],
)

kt_jvm_library(
    name = "bytestring_lib",
    srcs = ["src/main/kotlin/com/google/protobuf/ByteStrings.kt"],
    visibility = ["//java:__subpackages__"],
    deps = ["//java/lite"],
)

kt_jvm_library(
    name = "full_extensions",
    srcs = [
        "src/main/kotlin/com/google/protobuf/Anies.kt",
        "src/main/kotlin/com/google/protobuf/ExtendableMessageExtensions.kt",
    ],
    deps = ["//java/core"],
)

protobuf_versioned_kt_jvm_library(
    name = "kotlin_bundle",
    automatic_module_name = "com.google.protobuf",
    bundle_description = "Kotlin core Protocol Buffers library. Protocol " +
                         "Buffers are a way of encoding structured data in an" +
                         "efficient yet extensible format.",
    bundle_name = "Protocol Buffers [Kotlin-Core]",
    bundle_symbolic_name = "com.google.protobuf",
    visibility = ["//visibility:public"],
    exports = [
        ":bytestring_lib",
        ":full_extensions",
        ":only_for_use_in_proto_generated_code_its_generator_and_tests",
        ":shared_runtime",
        ":well_known_protos_kotlin",
    ],
)

kt_jvm_export(
    name = "kotlin_mvn",
    deploy_env = [
        "@rules_kotlin//kotlin/compiler:kotlin-stdlib",
        "//java/core",
    ],
    maven_coordinates = "com.google.protobuf:protobuf-kotlin:%s" % PROTOBUF_JAVA_VERSION,
    pom_template = "//java/kotlin:pom_template.xml",
    resources = [
        "//:java_features_proto",
        "//:well_known_type_protos",
        "//src/google/protobuf:descriptor_proto_srcs",
    ],
    tags = ["manual"],
    runtime_deps = [
        ":kotlin_bundle",
    ],
)

filegroup(
    name = "release",
    srcs = [
        ":kotlin_mvn-docs",
        ":kotlin_mvn-maven-source",
        ":kotlin_mvn-pom",
        ":kotlin_mvn-project",
    ],
    visibility = ["//java:__pkg__"],
)

test_suite(
    name = "tests",
    tests = [
        "bytestring_test",
        "proto2_test",
        "proto3_test",
        "shared_tests",
        "test_extensions",
    ],
)

kt_jvm_library(
    name = "bytestring_test_library",
    srcs = ["src/test/kotlin/com/google/protobuf/ByteStringsTest.kt"],
    deps = [
        ":bytestring_lib",
        "//java/lite",
        "@protobuf_maven//:com_google_truth_truth",
        "@protobuf_maven//:junit_junit",
        "@rules_kotlin//kotlin/compiler:kotlin-test",
    ],
)

java_test(
    name = "bytestring_test",
    test_class = "com.google.protobuf.kotlin.ByteStringsTest",
    runtime_deps = [":bytestring_test_library"],
)

proto_library(
    name = "example_extensible_message_proto",
    srcs = ["src/test/proto/com/google/protobuf/example_extensible_message.proto"],
    visibility = ["//java:__subpackages__"],
)

java_proto_library(
    name = "example_extensible_message_java_proto",
    deps = [":example_extensible_message_proto"],
)

kt_jvm_library(
    name = "shared_tests_library",
    srcs = [
        "src/test/kotlin/com/google/protobuf/DslListTest.kt",
        "src/test/kotlin/com/google/protobuf/DslMapTest.kt",
        "src/test/kotlin/com/google/protobuf/ExtensionListTest.kt",
    ],
    deps = [
        ":bytestring_lib",
        ":example_extensible_message_java_proto",
        ":only_for_use_in_proto_generated_code_its_generator_and_tests",
        ":shared_runtime",
        "@protobuf_maven//:com_google_guava_guava_testlib",
        "@protobuf_maven//:com_google_truth_truth",
        "@protobuf_maven//:junit_junit",
        "@rules_kotlin//kotlin/compiler:kotlin-test",
    ],
)

java_test(
    name = "shared_tests",
    test_class = "com.google.protobuf.kotlin.DslListTest",
    runtime_deps = [":shared_tests_library"],
)

kt_jvm_library(
    name = "test_extensions_library",
    srcs = [
        "src/test/kotlin/com/google/protobuf/AniesTest.kt",
        "src/test/kotlin/com/google/protobuf/ExtendableMessageExtensionsTest.kt",
    ],
    deps = [
        ":example_extensible_message_java_proto",
        ":full_extensions",
        ":kotlin_unittest",
        ":only_for_use_in_proto_generated_code_its_generator_and_tests",
        ":shared_runtime",
        "//java/core",
        "@protobuf_maven//:com_google_truth_truth",
        "@protobuf_maven//:junit_junit",
        "@rules_kotlin//kotlin/compiler:kotlin-test",
    ],
)

java_test(
    name = "test_extensions",
    test_class = "com.google.protobuf.kotlin.ExtendableMessageExtensionsTest",
    runtime_deps = [":test_extensions_library"],
)

proto_library(
    name = "evil_names_proto2",
    srcs = ["src/test/proto/com/google/protobuf/evil_names_proto2.proto"],
    visibility = ["//:__subpackages__"],
)

java_proto_library(
    name = "evil_names_proto2_java_proto",
    deps = [":evil_names_proto2"],
)

internal_gen_kt_protos(
    name = "gen_evil_names_proto2",
    deps = [":evil_names_proto2"],
)

proto_library(
    name = "evil_names_proto3",
    srcs = ["src/test/proto/com/google/protobuf/evil_names_proto3.proto"],
    visibility = ["//:__subpackages__"],
)

java_proto_library(
    name = "evil_names_proto3_java_proto",
    deps = [":evil_names_proto3"],
)

internal_gen_kt_protos(
    name = "gen_evil_names_proto3",
    deps = [":evil_names_proto3"],
)

proto_library(
    name = "multiple_files_proto3",
    srcs = ["src/test/proto/com/google/protobuf/multiple_files_proto3.proto"],
    visibility = ["//:__subpackages__"],
)

java_proto_library(
    name = "multiple_files_proto3_java_proto",
    deps = [":multiple_files_proto3"],
)

internal_gen_kt_protos(
    name = "gen_kotlin_proto3_java_multiple_files",
    deps = [":multiple_files_proto3"],
)

internal_gen_kt_protos(
    name = "gen_kotlin_unittest",
    visibility = ["//java:__subpackages__"],
    deps = ["//src/google/protobuf:kt_unittest_protos"],
)

kt_jvm_library(
    name = "kotlin_unittest",
    srcs = [
        ":gen_evil_names_proto2",
        ":gen_kotlin_unittest",
    ],
    deps = [
        ":evil_names_proto2_java_proto",
        ":only_for_use_in_proto_generated_code_its_generator_and_tests",
        ":shared_runtime",
        ":well_known_protos_kotlin",
        "//java/core",
        "//java/core:generic_test_protos_java_proto",
        "//java/core:lite_test_protos_java_proto",
    ],
)

internal_gen_kt_protos(
    name = "gen_kotlin_proto3_unittest",
    deps = ["//src/google/protobuf:kt_proto3_unittest_protos"],
)

kt_jvm_library(
    name = "kotlin_proto3_unittest",
    srcs = [
        ":gen_evil_names_proto3",
        ":gen_kotlin_proto3_java_multiple_files",
        ":gen_kotlin_proto3_unittest",
    ],
    deps = [
        ":evil_names_proto3_java_proto",
        ":multiple_files_proto3_java_proto",
        ":only_for_use_in_proto_generated_code_its_generator_and_tests",
        ":shared_runtime",
        "//java/core",
        "//java/core:generic_test_protos_java_proto",
        "//java/core:lite_test_protos_java_proto",
    ],
)

kt_jvm_library(
    name = "proto2_test_library",
    srcs = ["src/test/kotlin/com/google/protobuf/Proto2Test.kt"],
    deps = [
        ":kotlin_unittest",
        "//java/core:test_util",
        "@protobuf_maven//:com_google_truth_truth",
        "@protobuf_maven//:junit_junit",
        "@rules_kotlin//kotlin/compiler:kotlin-reflect",
    ],
)

java_test(
    name = "proto2_test",
    test_class = "com.google.protobuf.kotlin.Proto2Test",
    runtime_deps = [":proto2_test_library"],
)

kt_jvm_library(
    name = "proto3_test_library",
    srcs = ["src/test/kotlin/com/google/protobuf/Proto3Test.kt"],
    deps = [
        ":kotlin_proto3_unittest",
        "//java/core:test_util",
        "@protobuf_maven//:com_google_truth_truth",
        "@protobuf_maven//:junit_junit",
    ],
)

java_test(
    name = "proto3_test",
    test_class = "com.google.protobuf.kotlin.Proto3Test",
    runtime_deps = [":proto3_test_library"],
)

internal_gen_kt_protos(
    name = "gen_well_known_protos_kotlin",
    visibility = [
        "//java:__subpackages__",
    ],
    deps = [
        "//:any_proto",
        "//:api_proto",
        "//:compiler_plugin_proto",
        "//:descriptor_proto",
        "//:duration_proto",
        "//:empty_proto",
        "//:field_mask_proto",
        "//:java_features_proto",
        "//:source_context_proto",
        "//:struct_proto",
        "//:timestamp_proto",
        "//:type_proto",
        "//:wrappers_proto",
    ],
)

kt_jvm_library(
    name = "well_known_protos_kotlin",
    srcs = [
        ":gen_well_known_protos_kotlin",
    ],
    deps = [
        ":only_for_use_in_proto_generated_code_its_generator_and_tests",
        ":shared_runtime",
        "//java/core",
    ],
)

pkg_files(
    name = "dist_files",
    srcs = glob([
        "src/main/kotlin/com/google/protobuf/*.kt",
        "src/test/kotlin/**/*.kt",
        "src/test/proto/**/*.proto",
    ]) + [
        "BUILD.bazel",
        "generate-sources-build.xml",
        "generate-test-sources-build.xml",
        "pom.xml",
        "pom_template.xml",
    ],
    strip_prefix = strip_prefix.from_root(""),
    visibility = ["//java:__pkg__"],
)
